package com.example.foods.service.impl;

import com.example.foods.data.FoodsMapper;
import com.example.foods.data.OrderItemMapper;
import com.example.foods.domain.FoodsInfo;
import com.example.foods.entity.Foods;
import com.example.foods.entity.OrderItems;
import com.example.foods.domain.OrderItemsInfo;
import com.example.foods.service.OrderItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderItemServiceImpl implements OrderItemService {

    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    private FoodsMapper foodsMapper;

    @Override
    public List<OrderItemsInfo> list() {
        List<OrderItems> orderItemsList = orderItemMapper.list();

        List<OrderItemsInfo> orderItemsInfoList = new ArrayList<>();

        for (OrderItems orderItems : orderItemsList) {
            OrderItemsInfo orderItemsInfo = new OrderItemsInfo();
            orderItemsInfo.setOrderId(orderItems.getOrderId());
            orderItemsInfo.setFoodId(orderItems.getFoodId());
            orderItemsInfo.setQuantity(orderItems.getQuantity());
            orderItemsInfo.setPrice(orderItems.getPrice());
            //查询鲜花信息
            Foods foods = foodsMapper.findById(orderItems.getFoodId());
            if (foods != null) {
                orderItemsInfo.setFoodName(foods.getFoodName());
            }
            orderItemsInfoList.add(orderItemsInfo);
        }

        return orderItemsInfoList;
    }

    @Override
    public void delete(Integer id) {
        orderItemMapper.delete(id);
    }

    @Override
    public void addCart(FoodsInfo foodsInfo) {
        OrderItems orderItems = new OrderItems();
        orderItems.setFoodId(foodsInfo.getFoodId());
        orderItems.setPrice(foodsInfo.getPrice());
        //默认1
        orderItems.setQuantity(1);

        orderItemMapper.save(orderItems);
    }

}
